route.ts 2.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. import { ApiPath } from "@/app/constant";
  2. import { NextRequest } from "next/server";
  3. import { handle as openaiHandler } from "../../openai";
  4. import { handle as azureHandler } from "../../azure";
  5. import { handle as googleHandler } from "../../google";
  6. import { handle as anthropicHandler } from "../../anthropic";
  7. import { handle as baiduHandler } from "../../baidu";
  8. import { handle as bytedanceHandler } from "../../bytedance";
  9. import { handle as alibabaHandler } from "../../alibaba";
  10. import { handle as moonshotHandler } from "../../moonshot";
  11. import { handle as stabilityHandler } from "../../stability";
  12. import { handle as iflytekHandler } from "../../iflytek";
  13. import { handle as xaiHandler } from "../../xai";
  14. import { handle as proxyHandler } from "../../proxy";
  15. async function handle(
  16. req: NextRequest,
  17. { params }: { params: { provider: string; path: string[] } },
  18. ) {
  19. const apiPath = `/api/${params.provider}`;
  20. console.log(`[${params.provider} Route] params `, params);
  21. switch (apiPath) {
  22. case ApiPath.Azure:
  23. return azureHandler(req, { params });
  24. case ApiPath.Google:
  25. return googleHandler(req, { params });
  26. case ApiPath.Anthropic:
  27. return anthropicHandler(req, { params });
  28. case ApiPath.Baidu:
  29. return baiduHandler(req, { params });
  30. case ApiPath.ByteDance:
  31. return bytedanceHandler(req, { params });
  32. case ApiPath.Alibaba:
  33. return alibabaHandler(req, { params });
  34. // case ApiPath.Tencent: using "/api/tencent"
  35. case ApiPath.Moonshot:
  36. return moonshotHandler(req, { params });
  37. case ApiPath.Stability:
  38. return stabilityHandler(req, { params });
  39. case ApiPath.Iflytek:
  40. return iflytekHandler(req, { params });
  41. case ApiPath.XAI:
  42. return xaiHandler(req, { params });
  43. case ApiPath.OpenAI:
  44. return openaiHandler(req, { params });
  45. default:
  46. return proxyHandler(req, { params });
  47. }
  48. }
  49. export const GET = handle;
  50. export const POST = handle;
  51. export const runtime = "edge";
  52. export const preferredRegion = [
  53. "arn1",
  54. "bom1",
  55. "cdg1",
  56. "cle1",
  57. "cpt1",
  58. "dub1",
  59. "fra1",
  60. "gru1",
  61. "hnd1",
  62. "iad1",
  63. "icn1",
  64. "kix1",
  65. "lhr1",
  66. "pdx1",
  67. "sfo1",
  68. "sin1",
  69. "syd1",
  70. ];